<template>
  <VPage>
    <VTable ref="tableRef" v-bind="tableOptions">
      <template #form="{ form }">
        <el-input v-model="form.logistics_name" class="w-40" placeholder="物流商名称"></el-input>
        <VSelect v-model="form.status" placeholder="状态" :options="options.status" multiple collapse-tags filterable clearable style="min-width: 160px" />
        <VCascader v-model="form.update_user_id" placeholder="更新人" multiple collapse-tags filterable clearable style="min-width: 160px" />
        <VGroup>
          <div class="v-group-item">更新时间</div>
          <VDate v-model="form.dateRange.value" />
        </VGroup>
      </template>
      <template #toolbar_btns>
        <VButton auth="/purchase/settings/logistics/add" type="primary" @click="create(tableRef)"><i class="dk-iconfont icon-Plus" />添加</VButton>
        <VExport auth="/purchase/settings/logistics/derive" mainKey="lc_id" url="/v1/purchase-config/logistics-config/export" name="logistics" class="ml-auto"
          :tableRef="tableRef" />
      </template>
    </VTable>
    <Add ref="addRef" :options="options" @success="query" />
    <VLog ref="logRef" />
  </VPage>
</template>
  
<script setup lang="jsx" name="Account">
import api from './api'
import Add from './components/Add.vue'
import { getOption } from '@/api/public'
import { ElMessage, ElMessageBox } from 'element-plus'

const tableRef = ref()
const logRef = ref()
const loadings = reactive({})
// 查询
const query = () => {
  tableRef?.value.query()
}

// 新增
const addRef = ref()
const create = () => {
  addRef?.value.open()
}

// 删除
const change = ({ lc_id, status }, rowIndex) => {
  loadings['remove' + rowIndex] = true
  api.change({ lc_id, status: status === 1 ? 2 : 1 }).then(res => {
    query()
    ElMessage({ type: 'success', message: '操作成功!' })
  }).finally(() => {
    loadings['remove' + rowIndex] = false
  })
}

// 获取options
const options = ref({})
getOption({ type: 'audit_config' }).then(res => {
  options.value = res.data
})

// 列表配置
const tableOptions = reactive({
  showOverflow: false,
  formConfig: {
    data: {
      audit_name: '',
      purchase_account_id: [],
      account_type: [],
      dateRange: { type: 'update_time', value: [], range: true },
    }
  },
  columns: [
    { field: 'logistics_code', title: '物流编码', minWidth: 100 },
    { field: 'logistics_name', title: '物流商名称', minWidth: 200, slots: {
      default: ({row}) => <VText value={row.logistics_name} />
    } },
    { field: 'status_name', title: '状态', minWidth: 80, slots: {
      default: ({row}) => <VStatus value={row.status === 1} />
    } },
    { field: 'update_user', title: '更新人', minWidth: 100, slots: {
      default: ({row}) => <VText value={row.update_user} />
    } },
    { field: 'update_time', title: '更新时间', minWidth: 150, slots: {
      default: ({row}) => <VText value={row.update_time} />
    } },
    {
      title: '操作', width: 150, fixed: 'right', slots: {
        default: ({ row, rowIndex }) => {
          const { lc_id, status } = row
          return <div class='nm flex gap-2'>
            <VButton auth="/purchase/settings/logistics/edit" link type="primary" onClick={() => addRef?.value.open(lc_id)}>编辑</VButton>
            <VButton auth="/purchase/settings/logistics/enableordisable" link type="primary" loading={loadings['remove' + rowIndex]} onClick={() => change(row, rowIndex)}>{status === 1 ? '禁用' : '启用'}</VButton>
            <VButton auth="/purchase/settings/logistics/logs" link type="primary" onClick={() => logRef?.value.open({ log_type: 'logistics_config', lc_id })}>日志</VButton>
          </div>
        }
      }
    },
  ],
  proxyConfig: {
    autoLoad: false,
    ajax: {
      query: ({ page, form }) => api.query({ ...form, ...page })
    }
  }
})
</script>
  